package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.context.UserContext;
import com.example.demo.entity.Article;
import com.example.demo.entity.Post;
import com.example.demo.entity.User;
import com.example.demo.mapper.PostMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.PostLikeService;
import com.example.demo.service.PostService;
import com.example.demo.vo.PostVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PostLikeService postLikeService;

    @Override
    public IPage<PostVO> getPostList(Integer pageNum, Integer pageSize,String type) {
        Page<Post> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Post::getStatus, 0)
                .orderByDesc(Post::getCreateTime);
        // 添加查询条件
        if (type != null && !type.isEmpty()) {
            wrapper.eq(Post::getPostType, type);
        }
        // 执行分页查询
        IPage<Post> postPage = this.page(page, wrapper);

        // 转换为VO对象
        return postPage.convert(post -> {
            PostVO vo = new PostVO();
            BeanUtils.copyProperties(post, vo);

            // 获取发帖人信息
            User user = userMapper.selectById(post.getUserId());
            if (user != null) {
                vo.setUsername(user.getUsername());
                vo.setUserAvatar(user.getAvatar());
            }

            // 获取当前用户的点赞状态
            vo.setIsLiked(postLikeService.isPostLiked(post.getId(), UserContext.getUserId()));

            return vo;
        });
    }

    @Override
    public PostVO getPostDetail(Long id) {
        // 获取帖子信息
        Post post = this.getById(id);
        if (post == null) {
            throw new RuntimeException("帖子不存在");
        }

        // 转换为VO
        PostVO vo = new PostVO();
        BeanUtils.copyProperties(post, vo);

        // 获取发帖人信息
        User user = userMapper.selectById(post.getUserId());
        if (user != null) {
            vo.setUsername(user.getUsername());
            vo.setUserAvatar(user.getAvatar());
        }

        // 获取当前用户的点赞状态
        vo.setIsLiked(postLikeService.isPostLiked(id, UserContext.getUserId()));

        return vo;
    }

    @Override
    @Transactional
    public void createPost(Post post) {
        post.setCreateTime(LocalDateTime.now());
        post.setUpdateTime(LocalDateTime.now());
        post.setViewCount(0);
        post.setLikeCount(0);
        post.setCommentCount(0);
        post.setStatus(0);
        this.save(post);
    }

    @Override
    @Transactional
    public void updatePost(Post post) {
        Post existingPost = this.getById(post.getId());
        if (existingPost == null || existingPost.getStatus() == 1) {
            throw new RuntimeException("帖子不存在或已删除");
        }
        post.setUpdateTime(LocalDateTime.now());
        this.updateById(post);
    }

    @Override
    @Transactional
    public void deletePost(Long id) {
        Post post = this.getById(id);
        if (post == null || post.getStatus() == 1) {
            throw new RuntimeException("帖子不存在或已删除");
        }
        post.setStatus(1);
        post.setUpdateTime(LocalDateTime.now());
        this.updateById(post);
    }

    @Override
    @Transactional
    public void incrementViewCount(Long id) {
        Post post = this.getById(id);
        if (post == null || post.getStatus() == 1) {
            throw new RuntimeException("帖子不存在或已删除");
        }
        post.setViewCount(post.getViewCount() + 1);
        this.updateById(post);
    }
} 